package cn.tansanqinger.service.disposeresult.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tansanqinger.dao.sql.disposeresult.DisposeResultSqlDaoImpl;
import cn.tansanqinger.pojo.Disposeresult;
import cn.tansanqinger.pojo.User;
import cn.tansanqinger.service.disposeresult.IDisposeresultService;
import cn.tansanqinger.util.PageData;
import cn.tansanqinger.util.Tools;

@Service
public class DisposeresultServiceImpl implements IDisposeresultService {
	
	@Autowired
	private DisposeResultSqlDaoImpl impl;

	@Override
	public List<Disposeresult> selectDisposeresult(HttpServletRequest request) {
		String page = Tools.isEmpty(request.getParameter("page"));
		User user = (User) request.getSession().getAttribute("user");
		if(page.matches("\\d+")) {
			return impl.selectDisposeresults(user.getId(), Integer.parseInt(page));
		} else {
			return impl.selectDisposeresults(user.getId(), 1);
		}
	}

	@Override
	public int selectDisposeresultCount(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		return impl.selectIsSelectNumber(user.getId());
	}

	@Override
	public int DisposeresultCountAll(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		return impl.selectNumberAll(user.getId());
	}

	@Override
	public PageData selectDisposeresultOne(HttpServletRequest request) {
		String id = Tools.isEmpty(request.getParameter("id"));
		if(id.matches("\\d+")) {
			return impl.selectResultOne(Integer.parseInt(id));
		}
		return impl.selectResultOne(0);
	}

	@Override
	public PageData delete(HttpServletRequest request) {
		PageData resultMap = new PageData();
		String id = Tools.isEmpty(request.getParameter("id"));
		if(id.matches("\\d+")) {
			if(impl.delete(Integer.parseInt(id))==1) {
				resultMap.put("code","0");
				resultMap.put("msg", "删除成功");
			} else {
				resultMap.put("code","-1");
				resultMap.put("msg", "删除失败");
			}
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "数据错误");
		}
		return resultMap;
	}

}
